From d539b0411094f3af10ef691078b327befcc3d6f7 Mon Sep 17 00:00:00 2001 From: =?utf8?q?=C3=98yvind=20Kol=C3=A5s?= Date: Thu, 18 Aug 2005 18:36:44 +0000 Subject: [PATCH] fixed gamma correction issues --- ChangeLog | 20 +++++++++++++++++++ babl/base/model-gray.c | 10 ++++++++-- babl/base/model-grayscale.c | 10 ++++++++-- babl/base/model-rgb.c | 4 ++-- babl/base/model-ycbcr.c | 32 ++++++++++++----------------- babl/base/rgb-constants.h | 19 +++++++++++++++--- babl/base/util.h | 26 ++++++++++++++++++++++++ tests/rgb_to_lab_to_rgb.c | 40 ++++++++++++++++++++++++++++++++----- tests/rgb_to_ycbcr.c | 14 ++++++------- 9 files changed, 135 insertions(+), 40 deletions(-) diff --git a/ChangeLog b/ChangeLog index ea069a9..fccf39a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,23 @@ +2005-08-18 Øyvind Kolås + + * babl/base/util.h: added new and hopefully correct gamma correction + and uncorrection functions. + * babl/base/model-grayscale.c: (components), + (rgb_to_grayscale_2_2), (grayscale_2_2_to_rgb): register + "luminance-gamma2.2" component, and use new gamma functions. + * babl/base/model-rgb.c: (g3_gamma_2_2), (g3_inv_gamma_2_2): use new + gamma functions. + * babl/base/model-ycbcr.c: (components), (models), (rgb_to_ycbcr), + (ycbcr_to_rgb): use gamma corrected luminance, and correct gamma. (pixel_formats): + uncomment until a new data type exist with headroom/footrom in 8bit + for proper rescaling of 0..255 to 16..235 for luma and +/-112 range + with offset of 128 for chroma (16 thorugh 240 inclusive). + * babl/base/rgb-constants.h: more digits as well as some #if 0' dead + code for reference. + * tests/rgb_to_lab_to_rgb.c: add more colors to the test buffer. + * tests/rgb_to_ycbcr.c: set TOLERANCE to a more meaningful value, + updated the ycbcr version of 50% gray. + 2005-08-17 Øyvind Kolås * babl/babl-internal.h: (type_name##_id) babl_log upon failed diff --git a/babl/base/model-gray.c b/babl/base/model-gray.c index 20d31eb..821a75e 100644 --- a/babl/base/model-gray.c +++ b/babl/base/model-gray.c @@ -41,6 +41,12 @@ components (void) "id", BABL_LUMINANCE, "luma", NULL); + + babl_component_new ( + "luminance-gamma2.2", + "id", BABL_LUMINANCE_GAMMA_2_2, + "luma", + NULL); } static void @@ -137,7 +143,7 @@ rgb_to_grayscale_2_2 (int src_bands, luminance = red * RGB_LUMINANCE_RED + green * RGB_LUMINANCE_GREEN + blue * RGB_LUMINANCE_BLUE; - *(double*)dst[0] = pow (luminance, 2.2); + *(double*)dst[0] = linear_to_gamma_2_2 (luminance); if (dst_bands==2) *(double*)dst[1] = alpha; @@ -163,7 +169,7 @@ grayscale_2_2_to_rgb (int src_bands, double red, green, blue; double alpha; - luminance = pow (*(double *)src[0], (1.0F/2.2F)); + luminance = gamma_2_2_to_linear (*(double *)src[0]); red = luminance; green = luminance; blue = luminance; diff --git a/babl/base/model-grayscale.c b/babl/base/model-grayscale.c index 20d31eb..821a75e 100644 --- a/babl/base/model-grayscale.c +++ b/babl/base/model-grayscale.c @@ -41,6 +41,12 @@ components (void) "id", BABL_LUMINANCE, "luma", NULL); + + babl_component_new ( + "luminance-gamma2.2", + "id", BABL_LUMINANCE_GAMMA_2_2, + "luma", + NULL); } static void @@ -137,7 +143,7 @@ rgb_to_grayscale_2_2 (int src_bands, luminance = red * RGB_LUMINANCE_RED + green * RGB_LUMINANCE_GREEN + blue * RGB_LUMINANCE_BLUE; - *(double*)dst[0] = pow (luminance, 2.2); + *(double*)dst[0] = linear_to_gamma_2_2 (luminance); if (dst_bands==2) *(double*)dst[1] = alpha; @@ -163,7 +169,7 @@ grayscale_2_2_to_rgb (int src_bands, double red, green, blue; double alpha; - luminance = pow (*(double *)src[0], (1.0F/2.2F)); + luminance = gamma_2_2_to_linear (*(double *)src[0]); red = luminance; green = luminance; blue = luminance; diff --git a/babl/base/model-rgb.c b/babl/base/model-rgb.c index 8d5f28d..15b63e6 100644 --- a/babl/base/model-rgb.c +++ b/babl/base/model-rgb.c @@ -206,7 +206,7 @@ g3_gamma_2_2 (int src_bands, { int band; for (band=0;band<3;band++) - *(double*)dst[band] = pow (*(double*) src[band], 2.2); + *(double*)dst[band] = linear_to_gamma_2_2 (*(double*) src[band]); for (;band +#include #define BABL_PLANAR_SANITY \ { \ @@ -44,3 +45,28 @@ } #endif + +#define BABL_USE_SRGB_GAMMA + +#ifdef BABL_USE_SRGB_GAMMA + +static inline double +linear_to_gamma_2_2 (double value) +{ + if (value > 0.0030402477F) + return 1.055F * pow (value, (1.0F/2.4F)) - 0.055F; + return 12.92F * value; +} + +static inline double +gamma_2_2_to_linear (double value) +{ + if (value > 0.03928F) + return pow ((value + 0.055F) / 1.055F, 2.4F); + return value / 12.92F; +} + +#else + #define linear_to_gamma_2_2(value) (pow((value), (1.0F/2.2F))) + #define gamma_2_2_to_linear(value) (pow((value), 2.2F)) +#endif diff --git a/tests/rgb_to_lab_to_rgb.c b/tests/rgb_to_lab_to_rgb.c index cf4c870..eec4f27 100644 --- a/tests/rgb_to_lab_to_rgb.c +++ b/tests/rgb_to_lab_to_rgb.c @@ -22,16 +22,46 @@ #include "babl.h" #include "babl-internal.h" -#define PIXELS 6 +#define PIXELS 32 #define TOLERANCE 0.001 float source_buf [PIXELS*3]= - {0.0, 0.0, 0.0, - 0.5, 0.5, 0.5, + {1.0, 1.0, 1.0, 1.0, 1.0, 1.0, - 1.0, 0.0, 0.0, + 0.0, 1.0, 1.0, 0.0, 1.0, 0.0, - 0.0, 0.0, 1.0}; + 1.0, 0.0, 1.0, + 1.0, 0.0, 0.0, + 1.0, 0.0, 1.0, + 0.0, 0.0, 0.0, + + 0.75, 0.75, 0.75, + 0.75, 0.75, 0.75, + 0.0, 0.75, 0.75, + 0.0, 0.75, 0.0, + 0.75, 0.0, 0.75, + 0.75, 0.0, 0.0, + 0.75, 0.0, 0.75, + 0.0, 0.0, 0.0, + + 0.5, 0.5, 0.5, + 0.5, 0.5, 0.5, + 0.0, 0.5, 0.5, + 0.0, 0.5, 0.0, + 0.5, 0.0, 0.5, + 0.5, 0.0, 0.0, + 0.5, 0.0, 0.5, + 0.0, 0.0, 0.0, + + 0.25, 0.25, 0.25, + 0.25, 0.25, 0.25, + 0.0, 0.25, 0.25, + 0.0, 0.25, 0.0, + 0.25, 0.0, 0.25, + 0.25, 0.0, 0.0, + 0.25, 0.0, 0.25, + 0.0, 0.0, 0.0 + }; float temp_buf [PIXELS*3]; float destination_buf [PIXELS*3]; diff --git a/tests/rgb_to_ycbcr.c b/tests/rgb_to_ycbcr.c index 8462067..1208698 100644 --- a/tests/rgb_to_ycbcr.c +++ b/tests/rgb_to_ycbcr.c @@ -23,7 +23,7 @@ #include "babl-internal.h" #define PIXELS 6 -#define TOLERANCE 0.00000000000001 +#define TOLERANCE 0.000001 float source_buf [PIXELS*3]= {0.0, 0.0, 0.0, @@ -34,12 +34,12 @@ float source_buf [PIXELS*3]= 0.0, 0.0, 1.0}; float reference_buf [PIXELS*3]= - {0.0, 0.0, 0.0, - 0.5, 0.0, 0.0, - 1.0, 0.0, 0.0, - 0.299, -0.168736, 0.5, - 0.587, -0.331264, -0.418688, - 0.114, 0.5, -0.081312}; + {0.0, 0.0, 0.0, + 0.735357, 0.0, 0.0, + 1.0, 0.0, 0.0, + 0.299, -0.168736, 0.5, + 0.587, -0.331264, -0.418688, + 0.114, 0.5, -0.081312}; float destination_buf [PIXELS*3]; -- 2.30.2